package com.EAIPClub.CodeShellManager.service.impl;

import com.EAIPClub.CodeShellManager.mapper.CodeShellManagerHostListMapper;
import com.EAIPClub.CodeShellManager.service.CodeShellManagerService;
import com.api.POJO.CodeLab.CodeShellManager.CodeShellManagerHostBaseInfo;
import com.api.POJO.CodeLab.CodeShellManager.CodeShellManagerHost;
import com.api.POJO.CodeLab.CodeShellManager.CodeShellManagerReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/6
 */
@Service
@Slf4j
public class CodeShellManagerServiceImpl implements CodeShellManagerService {

    private CodeShellManagerHostListMapper mapper;

    @Autowired
    public void setMapper(CodeShellManagerHostListMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public CodeShellManagerReturnMsg applyForResource(String uid) {
        CodeShellManagerReturnMsg returnMsg = new CodeShellManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("用户uid:{},申请CodeShell资源.", uid);
        log.info("即将开始查询可使用的CodeShell节点.");
        List<CodeShellManagerHost> allCodeShellFreeHost = mapper.getAllCodeShellFreeHosts();
        if (allCodeShellFreeHost == null) {
            log.warn("查询可用CodeShell节点失败!返回为空!");
            returnMsg.setStatus(500);
            Msg.add("查找空闲节点失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查询结果为:{}", allCodeShellFreeHost);
        if (allCodeShellFreeHost.isEmpty()) {
            log.warn("未能找到空闲节点!即将拒绝用户资源申请.用户uid:{}", uid);
            returnMsg.setStatus(500);
            Msg.add("未能找到空闲节点!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        CodeShellManagerHost codeShellManagerHost = allCodeShellFreeHost.get(0);
        log.info("即将使用第一个节点,并改变其占用状态.{}", codeShellManagerHost);
        Boolean status = mapper.updateCodeIdeHostStatusById('N', codeShellManagerHost.getID());
        if (!status) {
            log.warn("修改该节点状态失败!host:{}", codeShellManagerHost);
            returnMsg.setStatus(500);
            Msg.add("未能正确申请节点!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("改变其占用状态成功.即将加入使用用户信息.uid:{}", uid);
        Boolean aBoolean = mapper.updateCodeIdeHostUserUidById(uid, codeShellManagerHost.getID());
        if (!aBoolean) {
            log.warn("加入使用用户信息失败!即将放弃该次申请.uid:{},host:{}", uid, codeShellManagerHost);
            mapper.updateCodeIdeHostStatusById('Y', codeShellManagerHost.getID());
            returnMsg.setStatus(500);
            Msg.add("加入使用用户信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("申请资源成功.本次业务结束.");
        returnMsg.setData(new CodeShellManagerHostBaseInfo(codeShellManagerHost.getIp(), codeShellManagerHost.getPort(), codeShellManagerHost.getUsername(), codeShellManagerHost.getPassword()));
        returnMsg.setStatus(200);
        return returnMsg;
    }

    @Override
    public CodeShellManagerReturnMsg setWebSocketSession(String session) {
        CodeShellManagerReturnMsg returnMsg = new CodeShellManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将加入session到数据库.session:{}", session);
        List<CodeShellManagerHost> allCodeShellNonSessionHosts = mapper.getAllCodeShellNonSessionHosts();
        if (allCodeShellNonSessionHosts == null) {
            log.warn("查找无session记录节点失败!返回为空!");
            returnMsg.setStatus(500);
            Msg.add("查找无session记录节点失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查找到session为空的节点为:{}", allCodeShellNonSessionHosts);
        CodeShellManagerHost codeShellManagerHost = allCodeShellNonSessionHosts.get(0);
        log.info("选择第一个session为空的节点为:{},即将加入session.", codeShellManagerHost);
        Boolean aBoolean = mapper.updateCodeIdeHostSessionById(session, codeShellManagerHost.getID());
        if (!aBoolean) {
            log.warn("加入session失败!session:{},host:{}", session, codeShellManagerHost);
            returnMsg.setStatus(500);
            Msg.add("加入session失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("加入session成功!本次业务结束.");
        returnMsg.setStatus(200);
        return returnMsg;
    }

    @Override
    public CodeShellManagerReturnMsg closeWebSocketSession(String session) {
        CodeShellManagerReturnMsg returnMsg = new CodeShellManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过session恢复host状态.session:{}", session);
        Boolean status = mapper.updateCodeIdeHostStatusBySession('Y', session);
        if (!status) {
            log.warn("通过session恢复host状态失败!session:{}", session);
            returnMsg.setStatus(500);
            Msg.add("通过session恢复host状态失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("即将通过session清除useruid.session:{}",session);
        Boolean aBoolean1 = mapper.updateCodeIdeHostUidBySession("", session);
        if(!aBoolean1){
            log.warn("通过session清除useruid失败!session:{}", session);
            returnMsg.setStatus(500);
            Msg.add("通过session清除useruid失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("即将通过session清除useruid.session:{}",session);
        Boolean aBoolean = mapper.updateCodeIdeHostSessionBySession("", session);
        if (!aBoolean){
            log.warn("通过session清除session失败!session:{}", session);
            returnMsg.setStatus(500);
            Msg.add("通过session清除session失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过session恢复host状态成功!本次业务结束.");
        returnMsg.setStatus(200);
        return returnMsg;
    }

    @Override
    public CodeShellManagerReturnMsg getCodeShellHostByUserUid(String useruid) {
        CodeShellManagerReturnMsg returnMsg = new CodeShellManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过useruid获取codeshell信息.useruid:{}",useruid);
        CodeShellManagerHost codeShellByUserUid = mapper.getCodeShellByUserUid(useruid);
        if(codeShellByUserUid == null){
            log.warn("通过useruid获取codeshell信息失败!返回为空!");
            returnMsg.setStatus(500);
            Msg.add("通过useruid获取codeshell信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        returnMsg.setData(new CodeShellManagerHostBaseInfo(codeShellByUserUid.getIp(), codeShellByUserUid.getPort(), codeShellByUserUid.getUsername(), codeShellByUserUid.getPassword()));
        returnMsg.setStatus(200);
        return returnMsg;
    }
}
